package paidaxing.blog.core.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import paidaxing.blog.common.result.Result;
import paidaxing.blog.core.dto.SettingDTO;
import paidaxing.blog.core.dto.SystemOverviewDTO;
import paidaxing.blog.core.dto.VisitStatDTO;
import paidaxing.blog.core.dto.SearchRecordDTO;
import paidaxing.blog.core.service.SettingsService;
import paidaxing.blog.core.service.VisitStatsService;
import paidaxing.blog.core.service.SearchRecordsService;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class SystemController {

    @Resource
    private SettingsService settingsService;

    @Resource
    private VisitStatsService visitStatsService;

    @Resource
    private SearchRecordsService searchRecordsService;

    /**
     * 获取系统配置
     *
     * @return 系统配置列表
     */
    @GetMapping("/settings")
    public Result<List<SettingDTO>> getSettings() {
        if (log.isInfoEnabled()) {
            log.info("获取系统配置");
        }
        
        List<SettingDTO> settings = settingsService.getAllSettings();
        return Result.success("获取系统配置成功", settings);
    }

    /**
     * 更新系统配置
     *
     * @param settingDTO 配置DTO
     * @return 更新结果
     */
    @SaCheckRole("ADMIN")
    @PutMapping("/settings")
    public Result<String> updateSetting(@RequestBody SettingDTO settingDTO) {
        if (log.isInfoEnabled()) {
            log.info("更新系统配置: {}", settingDTO);
        }
        
        // 检查用户是否登录且为管理员
        StpUtil.checkRole("ADMIN");
        
        boolean result = settingsService.updateSetting(settingDTO);
        if (!result) {
            return Result.failed("更新系统配置失败");
        }
        return Result.success("更新系统配置成功");
    }

    /**
     * 系统统计概览
     *
     * @return 系统统计概览
     */
    @GetMapping("/statistics/overview")
    public Result<SystemOverviewDTO> getSystemOverview() {
        if (log.isInfoEnabled()) {
            log.info("获取系统统计概览");
        }
        
        SystemOverviewDTO overview = visitStatsService.getSystemOverview();
        return Result.success("获取系统统计概览成功", overview);
    }

    /**
     * 访问统计数据
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 访问统计数据
     */
    @GetMapping("/statistics/visits")
    public Result<List<VisitStatDTO>> getVisitStats(
            @RequestParam(required = false) Date startDate,
            @RequestParam(required = false) Date endDate) {
        if (log.isInfoEnabled()) {
            log.info("获取访问统计数据，开始日期: {}，结束日期: {}", startDate, endDate);
        }
        
        List<VisitStatDTO> visitStats = visitStatsService.getVisitStatsByDateRange(startDate, endDate);
        return Result.success("获取访问统计数据成功", visitStats);
    }

    /**
     * 热门搜索关键词
     *
     * @param limit 限制数量，默认为10
     * @return 热门搜索关键词列表
     */
    @GetMapping("/statistics/search/hot")
    public Result<List<SearchRecordDTO>> getHotSearches(
            @RequestParam(defaultValue = "10") int limit) {
        if (log.isInfoEnabled()) {
            log.info("获取热门搜索关键词，限制数量: {}", limit);
        }
        
        List<SearchRecordDTO> hotSearches = searchRecordsService.getHotSearches(limit);
        return Result.success("获取热门搜索关键词成功", hotSearches);
    }

    /**
     * 搜索记录（按日期范围）
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 搜索记录列表
     */
    @SaCheckRole("ADMIN")
    @GetMapping("/statistics/search/records")
    public Result<List<SearchRecordDTO>> getSearchRecords(
            @RequestParam(required = false) Date startDate,
            @RequestParam(required = false) Date endDate) {
        if (log.isInfoEnabled()) {
            log.info("获取搜索记录，开始日期: {}，结束日期: {}", startDate, endDate);
        }
        
        // 检查用户是否登录且为管理员
        StpUtil.checkRole("ADMIN");
        
        List<SearchRecordDTO> searchRecords = searchRecordsService.getSearchRecordsByDateRange(startDate, endDate);
        return Result.success("获取搜索记录成功", searchRecords);
    }
}